;
(function($,win)
{
	/* @------------------------------------------------------------------------------------@ */
	$(document).ready(function()
	{
		debugger;
		var url = location.search;
		if (url.indexOf("?orderUuid=") != -1) {
			var reqPath = basePath+'order/queryOrderById'+url;
			var sendBody = '{}';
			send(reqPath, "POST", "application/json", "json", sendBody, function(res)
			{
				if (res.result == 'success')
				{
					if (res.data != null)
					{
						debugger;
						var order = res.data;
						var address = order.province+order.city+order.county+order.village+order.address;
						$("#uuid").val(order.orderUuid);
						$("#name").html(order.name);
						$("#phone").html(order.phone);
						$("#address").html(address);
						$("#requirement").html(order.requirement);
						initOrderHandleData(1,15);
					} else
					{
						alert("没有查找到相关数据");
					}
				}
			});
		}else if(url.indexOf("?orderId=") != -1)
		{
			debugger;
			$("#uuid").val(url.split('=')[1]);
			queryHandle(null);
		}else if(url.indexOf("?orderHandleUuid=") != -1)
		{
			debugger;
			var reqPath = basePath+'order/queryOrderHandleById'+url.split('&')[0];
			var sendBody = '{}';
			send(reqPath, "POST", "application/json", "json", sendBody, function(res)
			{
				if (res.result == 'success')
				{
					if (res.data != null)
					{
						var orderHandle = res.data;
						$("#uuid").val(url.split('&')[1].split('=')[1]);
						$("#orderHandleUuid").val(orderHandle.orderHandleUuid);
						$("#description").val(orderHandle.description);
						queryHandle(orderHandle.handleUuid);
					} else
					{
						alert("没有查找到相关数据");
					}
				}
			});
		}
		else
		{
			//初始化列表
			initOrderHandleData(1,15);
		}
		$('#addnew').click(function()
		{
			debugger;
			window.location.href = jumpPath + 'view/udwin/order/addOrderHandle.jsp?orderId='+$("#uuid").val();
		});

		// 查询
		$('#btn_search').bind('click', function()
		{
			initOrderHandleData(1,15);
		});

		$('#tbn_save').bind('click', function()
		{
			saveOrderHandle();
		});
		
		$('#tbn_update').bind('click', function()
		{
			updateOrderHandle();
		});

	});// the end

	/* @-----------------------------------------------------------------------------------------@ */
})(jQuery, window);

function queryHandle(handleUuid)
{
	var requsetPath = basePath + "handle/queryList";
	send(requsetPath, "POST", "application/json", "json", '{}',
	function(resultData)
	{
		if (resultData.result == 'success')
		{
			$.each(resultData.data, function(i,n)
			{
				$("#handle_uuid").append("<option value='"+n.handleUuid+"'>"+n.handleName+"</option>");
			});
			if(null!=handleUuid)
			{
				$("#handle_uuid").val(handleUuid);
			}
		} else
		{
			alert(resultData.errorMsg);
		}
	});
}

function editOrderHandle(orderHandleUuid)
{
	window.location.href = jumpPath + 'view/udwin/order/editOrderHandle.jsp?orderHandleUuid='+orderHandleUuid+'&orderUuid='+$("#uuid").val();
}

function del(id)
{
	if (confirm("确定要删除吗？"))
	{
		var reqPath = basePath+'order/deleteOrderHandle?orderHandleUuid='+id;
		send(reqPath, "POST", "application/json", "json", "{}", function(res)
		{
			if (res.result == 'success')
			{
				initOrderHandleData(1,15);
			}
		});
	}
}

function initOrderHandleData(pageNumber,pagesize)
{
	var reqPath = basePath+'order/queryOrderHandlePage';
	var sendBody = '{"curPage": 1,"pageSize": 15,"orderUuid":"'+$("#uuid").val()+'"';
	var $tab = $('#OrderHandleTables tbody').html("");

	
	var reqPath = basePath + 'order/queryOrderHandlePage';
	var sendBody = '{"curPage": '+pageNumber+',"pageSize": '+pagesize+',"orderUuid":"'+$("#uuid").val()+'"';
	var $tab = $('#OrderHandleTables tbody').html("");
	
	var requestData = $('#frm_orderhandle').serializeObject();
	requestData = JSON.stringify(requestData);
	sendBody = sendBody+requestData.replace('{',',');

	send(reqPath, "POST", "application/json", "json", sendBody, function(res)
	{

		if (res.result == 'success')
		{

			if (res.data.data.length > 0)
			{
				var tpl_tr = $('#tpl_list').html(), rows = '';

				$.each(res.data.data, function(i,n)
				{
					var operation="<button onclick='editOrderHandle(\""+n.orderHandleUuid+"\")' type='button' class='btn btn-primary'>编辑</button>"+
								  "<button onclick='del(\""+n.orderHandleUuid+"\")' type='button' class='btn btn-primary'>删除</button>";
					rows += tpl_tr.replace(/\$userName/g, n.userName)
							      .replace(/\$handleName/,n.handleName)
							      .replace(/\$description/, n.description)
							      .replace(/\$handleTime/, getLocalTime(n.handleTime.toString().substr(0,10)))
							      .replace(/\$operation/, operation);
				});
				$tab.append(rows);

				// page
				pagefunction(initOrderHandleData,res.data.totalPages,pagesize);

			} else
			{
				$tab.append('<tr><td colspan="7">没有查找到相关数据。</td></tr>');
			}
		}
	});

}

/**
 * 
 * @METHODNAME: saveLoginOrderHandler
 * @METDHOD:
 * @Description: 添加登陆用户
 * @author Administrator
 * @date 2015年1月10日 下午5:37:52
 */
function saveOrderHandle()
{
	
	var reqOrderHandletPath = basePath + "order/addOrderHandle";
	var requestData = $('#frm_orderHandle').serializeObject();
	requestData = JSON.stringify(requestData);
	send(reqOrderHandletPath, "POST", "application/json", "json", requestData,
	function(resultData)
	{

		if (resultData.result == 'success')
		{
			// 跳转到列表
			window.location = jumpPath
					+ 'view/udwin/order/listOrderHandle.jsp?orderUuid='+$("#uuid").val();
		} else
		{
			alert(resultData.errorMsg);
		}
	});
}


function updateOrderHandle()
{
	debugger;
	var reqOrderHandletPath = basePath + "order/updateOrderHandle";
	var requestData = $('#frm_orderhandle').serializeObject();
	requestData = JSON.stringify(requestData);
	send(reqOrderHandletPath, "POST", "application/json", "json", requestData,
	function(resultData)
	{
		debugger;
		if (resultData.result == 'success')
		{
			// 跳转到列表
			window.location = jumpPath
					+ 'view/udwin/order/listOrderHandle.jsp?orderUuid='+$("#uuid").val();
		} else
		{
			alert(resultData.errorMsg);
		}
	});
}
